/*
 * @Description: 路由模块
 * @Author: zheng yiliang
 * @Date: 2021-04-23 22:30:05
 * @LastEditTime: 2021-05-24 19:34:29
 * @LastEditors: zheng yiliang
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
// 路由数据
import routes from './routes'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css' // nprogress样式文件
// import Cookies from 'js-cookie'
NProgress.configure({
  easing: 'ease', // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: true, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
})

// fix vue-router NavigationDuplicated
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return VueRouterPush.call(this, location).catch(err => err)
}
const VueRouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (location) {
  return VueRouterReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const router = new VueRouter({
  routes,
  mode: 'history',
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
  // scrollBehavior (to, from, savedPosition) {
  //   if (to.hash) {
  //     return {
  //       selector: to.hash,
  //       behavior: 'smooth'
  //     }
  //   }
  // }
})

/**
 * 路由拦截
 * 权限验证
 */
router.beforeEach((to, from, next) => {
  console.log('路由拦截', to, from)
  // 开启进度条
  NProgress.start()
  // const hasToken = Cookies.get('TokenKey')
  // console.log(hasToken)
  // if (hasToken) {

  // }
  next()
})

router.afterEach((to, from) => {
  // 关闭进度条
  NProgress.done()
})

export default router
